package com.kcnet.qk.controller.addfriend;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.service.AutoAddPeopleService;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;
import static com.kcnet.qk.util.NodeUtil.findNodeByTextAndPerformClick;

/**
 * Created by Administrator on 2018/4/25 0025.
 * 添加通讯录好友
 */

public class AddFriendPhoneCTR extends BasePeopleController {

    public boolean init_imm;
    private static String TAG = "添加通讯录好友";

    private static AddFriendPhoneCTR mRongCloudInstance;


    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static AddFriendPhoneCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (AddFriendPhoneCTR.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new AddFriendPhoneCTR();
                }
            }
        }

        return mRongCloudInstance;
    }





    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击加好友请求模块");
            tagTextClick(FBStringUtil.getString(StringConst.TAB_JHYQQ));
            sleepTimedDefault();
            searchClick();
        }
        else if(!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.DWZ")){

            NLogger.d(TAG, "2、点击 搜索新好友");
            searchTypeClick(FBStringUtil.getString(StringConst.ADDFRIEND_SEATCH_NEW_FRIEND));
            initFbMain = true;
        }
        else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")){
            NLogger.d(TAG, "3、点击 通讯录");
            tagTextClick(FBStringUtil.getString(StringConst.ADDFRIEND_TXL));
            init_imm = true;
        }
        else if (init_imm &&(  mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            NLogger.d(TAG, "4、执行 自动添加好友");
             beginAddFriendRecommed();
        }else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.2Bh")){ //弹出对话框自动点击确认
            // mService.execShellCmd("input tap 197 141"); //随便模拟一次点击 消失对话框
            findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEREN));
        }
    }

    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriendRecommed(){
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {

             //   List<AccessibilityNodeInfo>  scrllorNodeInfos = NodeUtil.getidByNodeView(root,"list");
//                for(AccessibilityNodeInfo nodeInfo:scrllorNodeInfos){
//                    Log.d("ND","nodeInfo"+   nodeInfo.getClassName().toString());
//                }
                //Log.d("ND","点击了加好友"+   NodeUtil.performClick(as));
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                for(AccessibilityNodeInfo nodeInfo:scrllorNodeInfos){
                    Log.d("ND","nodeInfo"+   nodeInfo.getClassName().toString());
                }
                if(scrllorNodeInfos.size() > 2){
                    scrllorNodeInfo = scrllorNodeInfos.get(2);
                }
                if(scrllorNodeInfo == null){
                    stopTask();
                    //  PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, false);
                    //Log.i("czc", "scrllorNodeInfo 為空 停止任务 ");
                    return;
                }

                for(int q=0;q<scrllorNodeInfo.getChildCount();q++){

                    if(currentAddCount  == maxAddCount){
                        currentAddCount = 0;
                        stopTask();
                        //停止任务
                        break;
                    }

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    List<AccessibilityNodeInfo> list = as.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.ADDFRIEND_JIAWEIHAOYOU));

                    for(AccessibilityNodeInfo accessibilityNodeInfo:list){
                        sleepTimedDefault();
                        currentAddCount++;
                        NLogger.d(TAG,"点击了加为好友"+   NodeUtil.performClick(accessibilityNodeInfo));
                        break;
                    }

                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }

    public void setParms(int maxAddCount,int minAddTime,int maxAddTime ){
      //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.init_imm = false;
        this.initFbMain = false;
    }


}
